package cn.datawin.bms.modules.customer.service.impl;

import cn.datawin.bms.customer.entity.CstmCmppServerInfoEntity;
import cn.datawin.bms.deploy.entity.DeplServerModuleInfoEntity;
import cn.datawin.bms.modules.customer.mapper.CstmCmppServerInfoMapper;
import cn.datawin.bms.modules.customer.service.CstmCmppServerInfoService;
import cn.datawin.bms.modules.deploy.service.DeplServerModuleInfoService;
import cn.datawin.common.core.util.R;
import cn.datawin.dockerclient.dto.ActiveDto;
import cn.datawin.dockerclient.feign.RemoteDockerService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 下游cmpp-server服务器
 *
 * @author datawin-sms
 * @date 2025-06-18 09:39:02
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CstmCmppServerInfoServiceImpl extends ServiceImpl<CstmCmppServerInfoMapper, CstmCmppServerInfoEntity> implements CstmCmppServerInfoService {

	private final RemoteDockerService remoteDockerService;
	private final DeplServerModuleInfoService deplServerModuleInfoService;

	@Override
    public R saveServerInfo(CstmCmppServerInfoEntity cstmCmppServerInfo) {
		//1、校验cmpp-server服务器信息是否存在
		LambdaQueryWrapper<CstmCmppServerInfoEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(CstmCmppServerInfoEntity::getInnerIp, cstmCmppServerInfo.getInnerIp());
		wrapper.eq(CstmCmppServerInfoEntity::getServerPort, cstmCmppServerInfo.getServerPort());
		CstmCmppServerInfoEntity existEntity = baseMapper.selectOne(wrapper);
		if(existEntity != null){
			return R.failed("cmpp-server服务器信息已存在");
		}
		//1、保存cmpp-server服务器信息
		int insert = baseMapper.insert(cstmCmppServerInfo);
		if(insert <= 0){
			return R.failed("cmpp-server服务器信息保存失败");
		}
		//更新pkey =cs + id
		cstmCmppServerInfo.setPkey("cs" + cstmCmppServerInfo.getId());
		baseMapper.updateById(cstmCmppServerInfo);
		int moduleId = 1;//cmpp-server模块id默认是1
		int branchId = 1;//默认只有一个分支1
		Long id = cstmCmppServerInfo.getId();
		//1、通过docker-client服务，激活一个cmpp-server服务
		//获取选中服务器信息
		//插入一条记录
		DeplServerModuleInfoEntity entity = new DeplServerModuleInfoEntity();
		entity.setServerIp(cstmCmppServerInfo.getInnerIp());
		entity.setServerPort(cstmCmppServerInfo.getServerPort());
		entity.setStatus(0);
		entity.setBranchId(branchId);
		entity.setUpdatetime(LocalDateTime.now());
		entity.setModuleId(moduleId);
		boolean insert1 = deplServerModuleInfoService.save(entity);
		if(!insert1){
			log.error("serverModuleInfo保存失败");
			return R.failed("cmpp-server服务器信息保存失败");
		}
		DeplServerModuleInfoEntity serverModuleInfo = deplServerModuleInfoService.getById(entity.getId());
		//远程调用docker-client服务激活容器
		ActiveDto activeDto = new ActiveDto();
		activeDto.setModuleId(moduleId);
		activeDto.setBranchId(branchId);
		activeDto.setContainerName("CMPP-SERVER-" + id);
		activeDto.setConfigJson("{\"CMPP_PORT\":7891,\"pkey\":\""+cstmCmppServerInfo.getPkey()+"\"}");
		activeDto.setServerIp(serverModuleInfo.getServerIp());
		activeDto.setServerPort(serverModuleInfo.getServerPort());

		R result = remoteDockerService.active(activeDto);
		log.info("激活容器返回结果：{}", JSON.toJSONString(result));
		//激活失败
		if(result.getCode() != 0){
			return R.failed("remoteDockerService调用失败，激活容器失败");
		}
		//激活成功
		return R.ok();
	}
}
